package com.letian.web.controller.record;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.letian.common.annotation.Log;
import com.letian.common.core.controller.BaseController;
import com.letian.common.core.domain.AjaxResult;
import com.letian.common.enums.BusinessType;
import com.letian.record.domain.RecordCount;
import com.letian.record.service.IRecordCountService;
import com.letian.common.utils.poi.ExcelUtil;
import com.letian.common.core.page.TableDataInfo;

/**
 * 记录数量Controller
 * 
 * @author bmz
 * @date 2023-01-15
 */
@RestController
@RequestMapping("/record/count")
public class RecordCountController extends BaseController
{
    @Autowired
    private IRecordCountService recordCountService;

    /**
     * 查询记录数量列表
     */
    @PreAuthorize("@ss.hasPermi('record:count:list')")
    @GetMapping("/list")
    public TableDataInfo list(RecordCount recordCount)
    {
        startPage();
        List<RecordCount> list = recordCountService.selectRecordCountList(recordCount);
        return getDataTable(list);
    }

    /**
     * 查询记录数量列表
     */
    @PreAuthorize("@ss.hasPermi('record:count:list')")
    @GetMapping("/list/today")
    public TableDataInfo listToday(RecordCount recordCount)
    {
        startPage();
        recordCount.setCreateBy(getUsername());
        List<RecordCount> list = recordCountService.selectRecordCountListToday(recordCount);
        return getDataTable(list);
    }

    /**
     * 导出记录数量列表
     */
    @PreAuthorize("@ss.hasPermi('record:count:export')")
    @Log(title = "记录数量", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RecordCount recordCount)
    {
        List<RecordCount> list = recordCountService.selectRecordCountList(recordCount);
        ExcelUtil<RecordCount> util = new ExcelUtil<RecordCount>(RecordCount.class);
        util.exportExcel(response, list, "记录数量数据");
    }

    /**
     * 获取记录数量详细信息
     */
    @PreAuthorize("@ss.hasPermi('record:count:query')")
    @GetMapping(value = "/{countId}")
    public AjaxResult getInfo(@PathVariable("countId") Long countId)
    {
        return success(recordCountService.selectRecordCountByCountId(countId));
    }

    /**
     * 新增记录数量
     */
    @PreAuthorize("@ss.hasPermi('record:count:add')")
    @Log(title = "记录数量", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RecordCount recordCount)
    {
        recordCount.setUserId(getUserId());
        recordCount.setCreateBy(getUsername());
        return toAjax(recordCountService.insertRecordCount(recordCount));
    }

    /**
     * 修改记录数量
     */
    @PreAuthorize("@ss.hasPermi('record:count:edit')")
    @Log(title = "记录数量", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RecordCount recordCount)
    {
        recordCount.setUpdateBy(getUsername());
        Long count = recordCount.getUpdateCount() == null ? 1 : recordCount.getUpdateCount() + 1;
        recordCount.setUpdateCount(count);
        return toAjax(recordCountService.updateRecordCount(recordCount));
    }

    /**
     * 删除记录数量
     */
    @PreAuthorize("@ss.hasPermi('record:count:remove')")
    @Log(title = "记录数量", businessType = BusinessType.DELETE)
	@DeleteMapping("/{countIds}")
    public AjaxResult remove(@PathVariable Long[] countIds)
    {
        return toAjax(recordCountService.deleteRecordCountByCountIds(countIds));
    }
}
